/**
 * 项目中自定义hooks
 */
import { useEffect, useState } from 'react';

// 获取滚动高度
export const useScrollTop = () => {
  const [scrollTop, setScrollTop] = useState(0);

  useEffect(() => {
    function handleScroll() {
      const top =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      setScrollTop(top - 212);
    }
    handleScroll();
    window.addEventListener('click', handleScroll);

    return () => {
      window.removeEventListener('click', handleScroll);
    };
  }, []);

  return scrollTop;
};
// // 获取侧边距离
export const useSiderWidth = () => {
  const [siderWidth, setSiderWidth] = useState(0);
  useEffect(() => {
    const element = document.getElementsByClassName('ant-pro-sider-links')[0];
    function handleCount() {
      setTimeout(() => {
        const width = element.offsetWidth;
        setSiderWidth(width + 24);
      }, 300);
    }
    handleCount();
    element.addEventListener('click', handleCount);
    return () => {
      element.removeEventListener('click', handleCount);
    };
  }, []);

  return siderWidth;
};
